﻿using Dapper;
using System;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepTypeConfig;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.OA.WorkflowTemplate.WorkflowTemplateStepTypeConfig
{
	/// <summary>
	/// 工作流模板步骤类型配置
	/// </summary>
	public class WorkflowTemplateStepTypeConfigData : IWorkflowTemplateStepTypeConfigData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workflowtemplatesteptypeconfig";
		private const string QUERY = @"SELECT WorkflowTemplateStepTypeConfigId,TemplateCode,TemplateName,TemplateStepCode,Type,TypeName,FormOrder FROM oa_workflowtemplatesteptypeconfig";
		public WorkflowTemplateStepTypeConfigData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查工作流模板步骤类型配置是否存在
		public async Task<bool> Exists(long workflowTemplateStepTypeConfigId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateStepTypeConfigId=?WorkflowTemplateStepTypeConfigId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepTypeConfigId", workflowTemplateStepTypeConfigId, DbType.Int64);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加工作流模板步骤类型配置
		public async Task<WorkflowTemplateStepTypeConfigEntity> Add(WorkflowTemplateStepTypeConfigEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("oa_workflowtemplatesteptypeconfig", new string[] { "TemplateCode", "TemplateName", "TemplateStepCode", "Type", "TypeName", "FormOrder" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateStepTypeConfigId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?TemplateStepCode", entity.TemplateStepCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?Type", entity.Type?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TypeName", entity.TypeName?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?FormOrder", entity.FormOrder, DbType.Int32, size: 11);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateStepTypeConfigEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改工作流模板步骤类型配置
		public async Task<WorkflowTemplateStepTypeConfigEntity> Update(WorkflowTemplateStepTypeConfigEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("oa_workflowtemplatesteptypeconfig", new string[] { "TemplateCode", "TemplateName", "TemplateStepCode", "Type", "TypeName", "FormOrder" }, new string[] { "WorkflowTemplateStepTypeConfigId" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateStepTypeConfigId=?WorkflowTemplateStepTypeConfigId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepTypeConfigId", entity.WorkflowTemplateStepTypeConfigId, DbType.Int64, size: 20);
			parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?TemplateStepCode", entity.TemplateStepCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?Type", entity.Type?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TypeName", entity.TypeName?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?FormOrder", entity.FormOrder, DbType.Int32, size: 11);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateStepTypeConfigEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除工作流模板步骤类型配置
		public async Task<bool> Delete(long workflowTemplateStepTypeConfigId)
		{
			string sql = @"DELETE FROM oa_workflowtemplatesteptypeconfig WHERE WorkflowTemplateStepTypeConfigId=?WorkflowTemplateStepTypeConfigId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepTypeConfigId", workflowTemplateStepTypeConfigId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		public async Task<bool> Delete(string templateCode)
		{
			string sql = @"DELETE FROM oa_workflowtemplatesteptypeconfig WHERE TemplateCode=?TemplateCode";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?TemplateCode", templateCode, DbType.AnsiString, size: 50);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取工作流模板步骤类型配置
		public async Task<WorkflowTemplateStepTypeConfigEntity> GetEntity(long workflowTemplateStepTypeConfigId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateStepTypeConfigId=?WorkflowTemplateStepTypeConfigId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepTypeConfigId", workflowTemplateStepTypeConfigId, DbType.Int64);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowTemplateStepTypeConfigEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流模板步骤类型配置列表
		public async Task<IEnumerable<WorkflowTemplateStepTypeConfigEntity>> GetList(string templateCode)
		{
			string sql = string.Format(@"{0} WHERE TemplateCode=?TemplateCode", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?TemplateCode", templateCode, DbType.AnsiString, size: 50);
			return await this.databaseContext.Slave.QueryAsync<WorkflowTemplateStepTypeConfigEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流模板步骤类型配置分页
		public async Task<(IEnumerable<WorkflowTemplateStepTypeConfigEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workflowtemplatesteptypeconfig {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "WorkflowTemplateStepTypeConfigId" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowTemplateStepTypeConfigEntity, int>(sql, parameters);
		}
		#endregion
	}
}



